<?php

namespace SisesporteAdmin\Controller;

use SisesporteAdmin\Form\AlunoForm;
use Zend\View\Model\ViewModel;
use Zend\Barcode\Barcode;
//use Zend\Validator\File\IsImage;

class AlunoController extends CrudController
{

    public function __construct ()
    {
        $this->entity = "Sisesporte\Entity\Aluno";
        $this->form = "SisesporteAdmin\Form\AlunoForm";
        $this->service = "Sisesporte\Service\AlunoService";
        $this->controller = "aluno";
        $this->route = "sisesporte-admin";
    }

    protected $msg;

    public function newAction ()
    {
        $form = new AlunoForm(NULL, $this->getEm());

        $request = $this->getRequest();
        $entity = NULL;
        if ($request->isPost()) {
            
            $post = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()
            );

            $filter = new \Zend\Filter\File\RenameUpload($_SERVER['DOCUMENT_ROOT'] . "/upload/foto");
            $filter->setUseUploadName(true);
            
            $service = $this->getServiceLocator()->get($this->service);
            $entity = $service->insert($post);
            if ($entity) {
                $this->msg = TRUE;
                echo '<script src="/js/sisesporte-admin/aluno-new/mensagem.js"></script>';
            } else {
                return $this->redirect()->toRoute($this->route, array('controller' => $this->controller,
                            'action' => 'new'));
            }
        }

        return new ViewModel(array('form' => $form, 'id' => $entity, 'msg' => $this->msg));
    }

    public function editAction ()
    {

        $form = new AlunoForm(NULL, $this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
        }

        if ($request->isPost()) {
            
            $post = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()
            );
            
            @chmod('public/upload/foto', 0777);
            
            $files = $this->getRequest()->getFiles();
            
            $filter = new \Zend\Filter\File\RenameUpload(array(
                    'target' => "public/upload/foto",
                    'overwrite' => true
            ));
            $filter->setUseUploadName(true);
            $filter->filter($files['noImagem']);
            
            $form->setData($post);
            $service = $this->getServiceLocator()->get($this->service);
            $update = $service->update($this->params()->fromRoute('id', 0), $post);
            
            if ($update) {
                $this->msg = TRUE;
            }
        }
        
        return new ViewModel(array('form' => $form, 'entity' => $entity->getSqPessoa(), 'msg' => $this->msg));
    }

    public function viewAction ()
    {

        $form = new AlunoForm(NULL, $this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
        }

        if ($request->isPost()) {
            $form->setData($request->getPost());
            $service = $this->getServiceLocator()->get($this->service);
            $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());
        }
        return new ViewModel(array('form' => $form, 'entity' => $entity->getSqPessoa(), 'msg' => $this->msg));
    }

    public function emitAction ()
    {

        $form = new AlunoForm(NULL, $this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
        }

/*
        if ($request->isPost()) {
            $form->setData($request->getPost());
            $service = $this->getServiceLocator()->get($this->service);
            $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());
        }
*/

        $IdUsr = $this->params()->fromRoute('id', 0);
        
        
        return new ViewModel(array('form' => $form, 'entity' => $entity->getSqPessoa(), 'msg' => $this->msg, 'IdUsr' => $IdUsr));
    }    
    
    /**
     * Retorna Array de Cidades de acordo com o SqUf
     * @param type int $sqUf
     * @return array de Cidade
     */
    public function getCidadeByUfAction ()
    {
        if ($this->getRequest()->isPost()) {
            $param = $this->getRequest()->getPost();
            $service = $this->getServiceLocator()->get("Sisesporte\Service\CidadeService");
            $data = $service->getCidadeByUf($param['sqUf']);

            $viewModel = new ViewModel(array('data' => $data));
            return $viewModel->setTerminal(TRUE);
        }
    }

    public function barcodeAction()
    {

        // Only the text to draw is required
        $barcodeOptions = array('text' => $this->params()->fromRoute('id', 0));

        // No required options
        $rendererOptions = array();

        // Draw the barcode in a new image,
        // send the headers and the image
        Barcode::factory(
            'code39', 'image', $barcodeOptions, $rendererOptions
        )->render();           

    }
    
    
}
